<?php
namespace Home\Controller;
use Think\Controller;
class ProductController extends Controller {
    public function listindex(){
        $search = I('get.search/s','');
        $cate_id = I('get.cate_id/d',0);
        /**
         * 读取产品栏目
         */
        $listdata = M('category')->where(['type' => 2]) -> select();
        $list = make_tree($listdata);
        $this->assign('list',$list);

        /**
         * 读取相关藏品
         */
//        $datas = M('product')->field('id,title,images')->where(['cate_id' => $cate_id])->limit(5)->select();
        $datas = M('product') -> alias('p') -> field('p.id,p.cate_id,p.title,p.images,c.pid') -> join('left join dx_category c on p.cate_id = c.id') -> where('cate_id =' . $cate_id . ' OR pid=' . $cate_id) -> limit(5) -> select();
        $infos = [];
        foreach($datas as $key => $value){
            $infos[$key]['id'] = $value['id'];
            $infos[$key]['title'] = $value['title'];
            $infos[$key]['images'] = explode(',',$value['images']);
        }
//        dump($infos);exit;
        $this->assign('datasfv',$infos);
        array_pop($infos);
//        dump($infos);exit;
        $this->assign('datasfo',$infos);

        /**
         * 翻页产品
         */
        $datanum = 20;//一页显示的数据条数
        $where = empty($search)? '' : ' OR p.title like "%' . $search . '%"';
        $count = M('product')->where(['cate_id' => $cate_id])->count();
        $Page = new \Think\Page($count,$datanum);// 实例化分页类
        $Page->setConfig('next','下一页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('header','<span class="rows">共 %TOTAL_ROW% 条记录</span>');
        $show       = $Page->show();// 分页显示输出*/
        $data = M('product') -> alias('p') -> field('p.id,p.cate_id,p.title,p.images,c.pid') -> join('left join dx_category c on p.cate_id = c.id') -> where('cate_id =' . $cate_id . ' OR pid=' . $cate_id . $where) -> limit($Page->firstRow,$Page->listRows) -> select();
        $info = [];
        foreach($data as $key => $value){
            $info[$key]['id'] = $value['id'];
            $info[$key]['title'] = $value['title'];
            $info[$key]['images'] = explode(',',$value['images']);
        }
//        dump($info);exit;
        $this->assign('page',$show);
        $this->assign('datalist',$info);
        $this->display();
    }
    public function index(){
        /**
         * 读取产品分类列表在页面右侧展示
         */
        $listdata = M('category')->where(['type' => 2]) -> select();
        $list = make_tree($listdata);
//        dump($list);exit;
        $this->assign('list',$list);
        /**
         *读取产品详情在页面展示
         */
        $id = I('get.id/d',0);
        $data = M('product')->where(['id' => $id])->find();
        $title =M('category')->field('title')->where(['id' => $data['cate_id']])->find();
//        dump($data);exit;
        $image = explode(',',$data['images']);
        array_pop($image);
        $data['images'] = $image;
//        dump($image);exit;
//        dump($data);exit;
        $this->assign('title',$title['title']);
        $this->assign('data',$data);
        $this->assign('image',$image);
        /**
         * 读取相关藏品
         */
        $datas = M('product')->field('id,title,images')->where(['cate_id' => $data['cate_id']])->limit(5)->select();
        $info = [];
        foreach($datas as $key => $value){
            $info[$key]['id'] = $value['id'];
            $info[$key]['title'] = $value['title'];
            $info[$key]['images'] = explode(',',$value['images']);
        }
        $this->assign('datasfv',$info);
        array_pop($info);
        $this->assign('datasfi',$info);
        /**
         * 上一篇下一篇
         */
        $prev = M('product')->field('id,title')->where('id>'. $id . ' AND cate_id='.$data['cate_id'])->order('id')->find();
        $next = M('product')->field('id,title')->where('id<'. $id . ' AND cate_id='.$data['cate_id'])->order('id')->find();
        $this->assign('prev',$prev);
        $this->assign('next',$next);
        $this->display();
    }
}